package pen_exam.jingdong2023_11_11;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Class02_Tree {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < t; i++) {
            String[] s1 = sc.nextLine().split(" ");
            int n = Integer.parseInt(s1[0]);
            int x = Integer.parseInt(s1[1]);
            int[] in = new int[n + 1];
            HashMap<Integer,Integer> parent = new HashMap();
            for (int j = 0; j < n - 1; j++) {
                String[] s2 = sc.nextLine().split(" ");
                int from = Integer.parseInt(s2[0]);
                int to = Integer.parseInt(s2[1]);
                in[from]++;
                parent.put(to,from);
            }
            System.out.println(process(n,x,in,parent));
        }
    }
    public static boolean process(int n, int x,int[] in,HashMap<Integer,Integer> parent) {
        HashSet<Integer> zero = new HashSet<>();
        int user = 0;
        for (int i = 1; i <= n; i++) {
            if (in[i] == 0) {
                zero.add(i);
            }
        }
        if(zero.contains(x)){
            return true;
        }
        while(!zero.isEmpty()){
            user++;
            for (Integer z : zero) {
                in[parent.get(z)]--;
               if(in[parent.get(z)] == 0){
                   zero.add(parent.get(z));
               }
              // zero.remove(z);
            }
            if(user % 2 == 0 && zero.contains(x)){
                return true;
            }
            if(user % 2 == 1 && zero.contains(x)){
                return false;
            }
        }
        return false;
    }
}
